package com.xiaoyao.novel.crawl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;

import com.xiaoyao.novel.parser.BbqugeParser;
import com.xiaoyao.novel.pojo.Book;
import com.xiaoyao.novel.pojo.Chapter;
import com.xiaoyao.novel.util.ClassUtil;
import com.xiaoyao.novel.util.HttpUtil;

public class CopyOfBbqugeCrawl extends BaseCrawl {
	private static HttpUtil httpBook = new HttpUtil("GBK");
	private static HttpUtil httpChapter = new HttpUtil("UTF-8");
	private static Logger logger = Logger.getLogger(CopyOfBbqugeCrawl.class);

	public CopyOfBbqugeCrawl() {
		tag = Book.TYPE_BBQUGE;
	}
	public static String convertStreamToString(InputStream is) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}
	/**
	 * 爬一本书,目录
	 * 
	 * @param webId
	 */
	public void crawlBook(Book book) {
		// http://www.bbquge.com/book/55.html
		String describUrl = "http://www.bbquge.com/book/" + book.webId + ".html";
		String page = "";
		try {
			logger.info("读取简介:" + describUrl); // 
//			Map<String, String> headMap = new HashMap<String, String>();
//			page = httpBook.getPage(describUrl, headMap);
 
			HttpClient httpClient = new DefaultHttpClient();
			HttpGet httpGet = new HttpGet(describUrl);
			httpGet.addHeader( "Content-Type","text/html;charset=GBK");  
			HttpResponse httpResponse = httpClient.execute(httpGet);
			HttpEntity entity = httpResponse.getEntity();
			if (entity != null) {
				InputStream instreams = entity.getContent();
				page = convertStreamToString(instreams);
			}

			if (ClassUtil.isEmpty(book.describ)) {
				BbqugeParser.parseDescrib(page, book);
			}
			// http://www.bbquge.com/files/article/html/0/55/index.html
			String categoryUrl = book.bookUrl;
			logger.info("读取目录:" + categoryUrl);
			page = httpBook.getPage(categoryUrl);
			List<Chapter> list = BbqugeParser.parseBook(page, book);
			for (int i = book.savedCount; i < list.size(); i++) {
				Chapter chapter = book.list.get(i);
				crawlChapter(chapter);
			}
		} catch (Exception e) {
			logger.error(e);
		}
	}

	/**
	 * 爬章节类容
	 * 
	 * @param chapter
	 * @throws IOException
	 */
	public void crawlChapter(Chapter chapter) throws IOException {
		// http://www.bbquge.com/files/article/html/0/55/index.html
		String urlStr = chapter.chapterUrl;
		logger.info("爬取章节 :[" + urlStr + "]");
		String page = "";
		for (int i = 0; i < 3 && ClassUtil.isEmpty(page); i++) {
			try {
				page = httpBook.getPage(urlStr);
			} catch (Exception e) {
				// e.printStackTrace();
				logger.error(e);
			}
		}
		page = filter(page);
		BbqugeParser.parseChapter(page, chapter);
		logger.info("内容长度:" + chapter.chapterContent.length());
	}

	// ///////////////////////////////////////////
	// ///////////////////////////////////////////
	/**
	 * 过滤
	 * 
	 * @param page
	 * @return
	 */
	public String filter(String page) {
		page = page.replaceAll("<img.+?>", "");
		page = page.replaceAll("笔趣阁", "");
		page = page.replaceAll("&.{2,7};", "");
		page = page.replaceAll("www.bbquge.me", "");
		// 其他替换
		return page;
	}

	public static void main(String[] args) {
		Book book = new Book();
		book.bookId = 63;
		book.webId = "55"; //
		CopyOfBbqugeCrawl crawl = new CopyOfBbqugeCrawl();
		crawl.crawlBook(book);
		// System.out.println(System.currentTimeMillis());
	}

}
